import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common_app/event_bus/audio_bottom_event.dart';
import 'package:flutter_common_app/event_bus/global_event_bus.dart';
import 'package:flutter_common_app/provider_model/audio_model.dart';
import 'package:flutter_common_app/widgets/empty_view/v_empty_view.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PlayListDialog extends StatefulWidget {
  final bool isShowAudioBottom;

  const PlayListDialog(this.isShowAudioBottom, {Key key}) : super(key: key);

  @override
  _PlayListDialogState createState() => _PlayListDialogState();
}

class _PlayListDialogState extends State<PlayListDialog> {
  @override
  void initState() {
    if (widget.isShowAudioBottom) {
      GlobalEventBus().bus.fire(AudioBottomEvent(false));
    }
    super.initState();
  }

  @override
  void dispose() {
    if (widget.isShowAudioBottom) {
      GlobalEventBus().bus.fire(AudioBottomEvent(true));
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<AudioModel>(builder: (context, AudioModel model, child) {
      return Container(
        width: 750.w,
        height: 1334.h,
        color: Colors.transparent,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              padding: EdgeInsets.fromLTRB(30.w, 20.w, 30.w, 0),
              height: ScreenUtil().screenHeight * 0.75,
              width: 690.w,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(
                  Radius.circular(30.w),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Text(
                        '当前播放',
                        style: TextStyle(
                          decoration: TextDecoration.none,
                          color: Colors.black,
                          fontSize: 34.sp,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        '(${model.playList.length})',
                        style: TextStyle(
                            decoration: TextDecoration.none,
                            fontSize: 26.sp,
                            color: Colors.black54,
                            fontWeight: FontWeight.normal),
                      ),
                    ],
                  ),
                  VEmptyView(10.w),
                  Expanded(
                    child: ListView.builder(
                      itemBuilder: (context, index) {
                        return GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            model.playOneSong(model.playList[index]);
                          },
                          child: Container(
                            height: 100.w,
                            child: Row(
                              children: [
                                if (model.currentSongData ==
                                    model.playList[index])
                                  Icon(
                                    Icons.pause_circle_outline,
                                    color: Colors.red,
                                    size: 20,
                                  ),
                                Expanded(
                                  child: Text.rich(
                                    TextSpan(children: [
                                      TextSpan(
                                        text: model.playList[index]?.name,
                                        style: TextStyle(
                                          fontSize: 30.sp,
                                          decoration: TextDecoration.none,
                                          color: Colors.black,
                                          fontWeight: FontWeight.normal,
                                        ),
                                      ),
                                      TextSpan(
                                        text:
                                            '-${model.playList[index]?.artists}',
                                        style: TextStyle(
                                          fontSize: 20.sp,
                                          decoration: TextDecoration.none,
                                          color: Colors.black54,
                                          fontWeight: FontWeight.normal,
                                        ),
                                      ),
                                    ]),
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ),
                                GestureDetector(
                                  behavior: HitTestBehavior.opaque,
                                  child: Icon(
                                    Icons.close,
                                    color: Colors.black54,
                                    size: 20,
                                  ),
                                  onTap: () {
                                    model.deleteSong(model.playList[index]);
                                  },
                                ),
                              ],
                            ),
                          ),
                        );
                      },
                      itemCount: model.playList.length,
                    ),
                  ),
                ],
              ),
            ),
            VEmptyView(30.w),
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              child: Icon(
                Icons.close,
                color: Colors.white,
              ),
              onTap: () {
                Navigator.pop(context);
              },
            ),
          ],
        ),
      );
    });
  }
}
